package com.sywb.chuangyebao.bean;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;

import org.bining.footstone.adapter.entity.MultiItemEntity;
import org.bining.footstone.log.Logger;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class QuestionInfo extends MultiItemEntity implements Serializable {

    public int item_id;// ItemID
    public String item_type;// Item类型(question)
    public String category_id;// 所属分类ID 多个用[,]隔开
    public String categoryName;// 所属分类名称
    public long topic_id;// 所属话题ID
    public String topic_name;// 所属话题名称
    public String title;// 提问内容
    public List<QARichInfo> richInfoList;// 提问@用户列表
    public List<String> pic;// 图片路径列表
    public String audio;// 语音路径
    public String audioLength;// 语音时长
    public long create_time;// 提问时间
    public String answer;// 最新回答

    public String web_url;// 网页分享链接
    public String mobi_url;// 移动分享链接

    public int uid;// 提问用户ID
    public int projectid;// 项目ID
    public String flag;

    public NewsCount count;// 提问相关计数
    public NewsOwner owner;// 提问用户信息

    public int progress;
    public int max;

    public void setItemType() {
        if (!TextUtils.isEmpty(audio)) {
            itemType = Constants.QANDA_MEDIA;
            return;
        }
        if (pic != null && pic.size() > 0) {
            if (pic.size() == 1) {
                itemType = Constants.QANDA_PIC_ONE;
            } else {
                itemType = Constants.QANDA_PIC_THREE;
            }
            return;
        }
        itemType = Constants.QANDA_TEXT;
    }

    public void setTopic() {
        if (topic_id > 0 && topic_name != null && topic_name.length() > 0) {
            String topic = "#" + topic_name + "#";

            boolean isAdd = true;
            if (title.startsWith(topic)) {
                if (richInfoList != null && richInfoList.size() > 0) {
                    HashMap<Integer, QARichInfo> richMap = new HashMap<>();

                    for (int i = 0; i < richInfoList.size(); i++) {
                        QARichInfo richInfo = richInfoList.get(i);
                        if (richInfo == null || TextUtils.isEmpty(richInfo.richName)) {
                            continue;
                        }
                        Logger.e("richMap put:" + JSON.toJSONString(richInfo));
                        richMap.put(richInfo.position, richInfo);
                    }

                    if (richMap.containsKey(0)) {
                        QARichInfo richInfo = richMap.get(0);
                        Logger.e("srtTopic:" + JSON.toJSONString(richInfo));
                        if (richInfo.richName.equals(topic)) {
                            isAdd = false;
                        }
                    }
                }
            } else {
                title = topic + title;
            }

            if (isAdd) {
                QARichInfo qaRichInfo = new QARichInfo();
                qaRichInfo.uid = (int) topic_id;
                qaRichInfo.richName = topic;
                qaRichInfo.position = 0;

                if (richInfoList == null) {
                    richInfoList = new ArrayList<>();
                } else {
                    for (QARichInfo q : richInfoList) {
                        q.position = q.position + topic.length();
                    }
                }
                richInfoList.add(qaRichInfo);
            }
        }
    }

}
